package com.jason.library.crash;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import com.jason.library.common.utils.LogUtils;

/**
 * Description:LifecycleCallback
 *
 * @author 陈宝阳
 * @create 2020/11/17 16:40
 */
public class LifecycleCallback implements Application.ActivityLifecycleCallbacks {

    public static LifecycleCallback getInstance() {
        return HolderClass.INSTANCE;
    }


    private final static class HolderClass {
        private final static LifecycleCallback INSTANCE = new LifecycleCallback();
    }


    private LifecycleCallback() {
    }

    /**
     * 必须在 Application 的 onCreate 方法中调用
     */
    public void init(Application application) {
        application.registerActivityLifecycleCallbacks(this);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        LogUtils.i("Activity生命周期", "onActivityCreated");
        ActivityStackManager.getInstance().addActivity(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        LogUtils.i("Activity生命周期", "onActivityStarted");
    }

    @Override
    public void onActivityResumed(Activity activity) {
        LogUtils.i("Activity生命周期", "onActivityResumed");
    }

    @Override
    public void onActivityPaused(Activity activity) {
        LogUtils.i("Activity生命周期", "onActivityPaused");
    }

    @Override
    public void onActivityStopped(Activity activity) {
        LogUtils.i("Activity生命周期", "onActivityStopped");
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        LogUtils.i("Activity生命周期", "onActivitySaveInstanceState");
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        LogUtils.i("Activity生命周期", "onActivityDestroyed");
        //将当前Activity移除到容器
        ActivityStackManager.getInstance().removeActivity(activity);
    }
}
